package com.fx.eduservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fx.commonuntils.ErrorEnum;
import com.fx.commonuntils.ReturnMessage;

import com.fx.commonuntils.ReturnMessageUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fx.eduservice.entity.vo.EduTeacher;
import com.fx.eduservice.request.TeacherQuery;
import com.fx.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-04
 */
//未上传
@Api(description = "讲师管理系统")
@RestController
@RequestMapping("/eduservice/edu-teacher")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
@CrossOrigin
public class EduTeacherController {

    final private EduTeacherService eduTeacherService;

    //查询所有讲师列表
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public ReturnMessage<Object> findAll() {

        List<EduTeacher> list = eduTeacherService.list(null);

        return ReturnMessageUtils.success(list);
    }

    //根据id删除讲师
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/removeTeacher/{id}")
    public ReturnMessage<Object> removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true)
                                               @PathVariable String id) {


        return ReturnMessageUtils.success(eduTeacherService.removeById(id));


    }


    @ApiOperation(value = "无条件分页查询讲师")
    @GetMapping("/pageTeacher/{pageNo}/{pageSize}")
    public ReturnMessage<Page<EduTeacher>> pageListTeacher(@ApiParam(name = "pageNo", value = "页数", required = false)
                                                           @PathVariable long pageNo,
                                                           @ApiParam(name = "pageSize", value = "条数", required = false)
                                                           @PathVariable long pageSize) {


        //创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(pageNo, pageSize);

        eduTeacherService.page(pageTeacher, null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();


        return ReturnMessageUtils.success(pageTeacher);


    }

    @ApiOperation(value = "有条件分页查询讲师")
    @PostMapping("/pageTeacherCondition")
    public ReturnMessage<IPage<EduTeacher>> pageTeacherCondition(@RequestBody @Valid TeacherQuery request,
                                                                 BindingResult br) {


        log.info("Request:{}", request);
        if (request == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return eduTeacherService.pageTeacherCondition(request);


    }

    @ApiOperation(value = "新增/更新讲师")
    @PostMapping("/addTeacher")
    public ReturnMessage<Object> addTeacher(@RequestBody @Valid EduTeacher eduTeacher,
                                            BindingResult br) {


        log.info("Request:{}", eduTeacher);
        if (eduTeacher == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        //boolean saveOrUpdate = eduTeacherService.saveOrUpdate(eduTeacher);
        boolean saveOrUpdate = eduTeacherService.saveOrUpdate(eduTeacher);
        if (saveOrUpdate) {
            return ReturnMessageUtils.success("新增讲师成功", eduTeacher.getId());
        } else {
            return ReturnMessageUtils.error(ErrorEnum.E_EDU_TEACHER.getErrorCode(), ErrorEnum.E_EDU_TEACHER.getErrorMsg(), saveOrUpdate);
        }


    }


}

